﻿#ifndef PACKAGE_H
#define PACKAGE_H

#include <cv.h>
#include <highgui.h>


#include "jsbytearray.h"
#include "../DIB/dib.h"
#include "../Public/windows.h"
#include "../Public/public.h"

//#include <netinet/in.h>
#include <list>
using namespace std;


#define MSG_ALARM        1
#define MSG_ALARMACK     2
#define MSG_TRADEINFO    3
#define MSG_IMAGE        4
#define MSG_SYSTEMSTATUS 5
#define MSG_QUERYSYSTEMSTATUS 6
#define MSG_SYNCSYSTEMTIME    7
#define MSG_CONFIG_NOPERSONTIME 8
#define MSG_CAMERABLOCK 9


//交易信息数据包
struct ATM_FaceInfo
{
    ATM_FaceInfo()
    {
        ZeroMemory(this, sizeof(ATM_FaceInfo));
    }

    char ICcardno[32];                        //卡号
    BYTE ATMno;				   		          //ATM编号
    BYTE cardno;					          //摄像机编号（水平=0x00/垂直=0x01/全景=0x02）
    short addr;				                  //地点编号（后端根据编号查找出对应的地点信息）
    long time;								  //交易时间
    unsigned int originImageLen;			  //原始图片长度
    unsigned int faceImageLen;       		  //人脸截图长度
};

typedef list<CvRect> Rects;

//报警信息数据包
struct ATM_AlarmInfo
{
    ATM_AlarmInfo()
    {
        ZeroMemory(this, sizeof(ATM_AlarmInfo));
    }
    long time;								  //报警时间
    DWORD warnID;					          //报警编号
    BYTE ATMno;				                  //ATM编号
    BYTE cardno;					          //摄像机编号（水平=0x00/垂直=0x01/全景=0x02）
    short addr;				                  //地点编号
    BYTE type;                                //键盘异常、遗留物异常、出钞口异常、ATM柜员机周围异常
    BYTE level;						          //报警级别
    unsigned int exImageLen;				  //异常图像长度（垂直或全景摄像机拍摄）
    short rectNum;                            //框的数目
    //Rects rects;                            //框的坐标（链表）
};

struct AlarmACK
{
    AlarmACK()
    {
        ZeroMemory(this, sizeof(AlarmACK));
    }
    DWORD warnID;					  //报警编号(从AlarmInfo中提取)
    BYTE cardno;					  //摄像机编号（水平=0x00/垂直=0x01/全景=0x02）(从AlarmInfo中提取)
    BYTE updateBkg;                   //改新背景标志 0x00:不改新背景; 0x01:改新背景
};					

//实时图像数据包
struct ImageInfo
{
    ImageInfo()
    {
        ZeroMemory(this, sizeof(ImageInfo));
    }

    long time;						  //图像时间
    BYTE ATMno;				          //ATM编号
    BYTE cardno;					  //摄像机编号（水平=0x00/垂直=0x01/全景=0x02）
    short addr;				          //地点编号
    BYTE type;						  //图像类型
    int len;				  //图像长度
};

//系统状态包
struct SystemStatus
{
    SystemStatus()
    {
        ZeroMemory(this, sizeof(SystemStatus));
    }

    long time;                        //系统时间，4字节
    BYTE cpu;                         //CPU占用率             
    BYTE mem;                         //内存占用率
    BYTE ATMno;                       //ATM编号（水平=0x00/垂直=0x01/全景=0x02）
    BYTE hVideoStatus;                //水平摄像机状态(0x00=状态异常/0x01=状态正常)
    BYTE vVideoStatus;                //垂直摄像机状态(0x00=状态异常/0x01=状态正常)
};


struct CameraBlockPack
{
    CameraBlockPack()
    {
        ZeroMemory(this, sizeof(CameraBlockPack));
    }

    long time;      //block time
    IplImage *pImg; //block image
};


void cvIplToJpeg(IplImage * pImg, BYTE jpeg[], int & jpegSize, const char* filename);

int PackImage(CJSByteArray &byteArray, ImageInfo &image, IplImage &lplImage);
int PackImage(CJSByteArray &byteArray, ImageInfo &image, CDIB &fdib);

int PackAlarm(CJSByteArray &byteArray, ATM_AlarmInfo &alarm, Rects &rects, IplImage &lplImage);
int PackAlarm(CJSByteArray &byteArray, ATM_AlarmInfo &alarm, Rects &rects, CDIB &fdib);

int PackTradeInfo(CJSByteArray &byteArray, ATM_FaceInfo &trade, IplImage &originlplImage, IplImage &facelplImage);
int PackTradeInfo(CJSByteArray &byteArray, ATM_FaceInfo &trade, CDIB &originfdib, CDIB &facefdib);

int PackSystemStatus(CJSByteArray &byteArray, SystemStatus &system);

int PackCameraBlockPack(CJSByteArray &byteArray, CameraBlockPack &cameraBlock);

BYTE check(BYTE* byte, int length);


#endif

